package com.sleepingcat.flutter_boost_dart.flutter_boost_dart

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.containers.FlutterBoostFragment
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.FlutterFragmentActivity

/**
 * @作者 志浩
 * @时间 2025/2/28 22:01
 * @描述 TODO
 */
class MyFlutterActivity : FlutterFragmentActivity() {
    private val TAG = "FlutterActivity"

    companion object {
        fun startActivity(context: Context, uniqueId: String?, url: String?, urlParams: HashMap<String, Any>) {
            val intent = Intent(context, MyFlutterActivity::class.java)
            intent
                .putExtra("uniqueId", uniqueId)
                .putExtra("url", url)
                .putExtra("urlParams", urlParams)
            FlutterBoost.instance().currentActivity().startActivity(intent)
        }
    }

    private var flutterFragment: FlutterBoostFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun createFlutterFragment(): FlutterFragment {
        val uniqueId = intent.getStringExtra("uniqueId")
        val url = intent.getStringExtra("url")
        val urlParams = intent.getSerializableExtra("urlParams") as Map<String, Any>
        flutterFragment = MyFlutterFragment.build(uniqueId, url, urlParams)
        return flutterFragment!!
    }

    override fun onBackPressed() {
        if (flutterFragment != null) {
            flutterFragment!!.onBackPressed()
        } else {
            super.onBackPressed()
        }
    }
}